// pages/user/page/setting/account/account.js

var app = getApp()
const db = wx.cloud.database()
const _ = db.command
const userInformationDB=db.collection('userlogin')

Page({

  /**
   * 页面的初始数据
   */
  data: {
      phone:null,
      ispwdformat:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({//向界面传值（用户电话、用户名、头像）
      phone: app.userDate.userInfo.userPhone,
    })
  },
  firstpwd: function (res) {
    console.log(res.detail.value)
    var reg = /^(\w){6,20}$/;
    if (!reg.test(res.detail.value)) {//验证密码格式
      this.setData({
        ispwdformat: false,
      })
      wx.showToast({
        title: '密码格式错误',
        icon:'none'
      })
    } else {
      this.setData({
        ispwdformat: true,
      })
    }
  },
  modifyPwd:function(res){
    let form_kv = res.detail.value
    console.log(form_kv)
    if(form_kv.newPwd==form_kv.newPwd2){
      if(this.data.ispwdformat){
      userInformationDB.where({
        userPhone:form_kv.phone,
        userPwd:form_kv.oldPwd
      }).get().then(res => {
      console.log(res)
      if(res.data==''){
        wx.showToast({
          title: '旧密码错误',
          icon:'none'
        })
      }
      else{//修改密码
       wx.showLoading({
         title: '正在修改...',
       })
        wx.cloud.callFunction({
          name:'update',
          data:{
            phone:form_kv.phone,
            pwd:form_kv.newPwd
          },success: res => {
            console.log(res)
            wx.hideLoading()
            wx.showToast({
              title: '修改成功',
              icon:'success',
              duration:3000
            })
            wx.navigateBack()
          },
          fail: err => {
            console.log(err)
            wx.hideLoading()
            wx.showToast({
              title: '修改出错',
              icon:'none'
            })
          }
        })
      }
    })
  }else{
    wx.showToast({
      title: '密码格式错误',
      icon:'none'
    })
  }
    }else{
      wx.showToast({
        title: '两次输入密码不一致',
        icon:'none'
      })
    }
  },

  
})